package controller.expertInfo.adminEdit;

import com.baidu.unbiz.fluentvalidator.ComplexResult;
import com.baidu.unbiz.fluentvalidator.FluentValidator;
import com.baidu.unbiz.fluentvalidator.ResultCollectors;
import controller.common.BaseController;
import controller.common.StaticConstant;
import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import response.CommonResult;
import services.dictionary.CertificateTypeServices;
import services.expertInfo.ExpertReviewServices;
import util.JsonUtil;
import validator.EndTimeFluentValidator;
import wt.entity.expertInfo.ExpertReview;

import java.util.List;
import java.util.Map;

;

/**
 * 功能:
 * 作者: 洪世成
 * 日期: 2017/9/9
 * 版权所有: admin
 */
@Controller

@RequestMapping("expertInfo/adminEdit/expertReview")
public class ExpertReviewController extends BaseController {

    @Autowired
    ExpertReviewServices expertReviewServices;
    @Autowired
    CertificateTypeServices certificateTypeServices;

    @RequiresPermissions("expertInfo:adminEdit:show")
    @RequestMapping("index")
    public String index(String expertId, Map map) throws Exception {

        List<ExpertReview> expertReviewList = expertReviewServices.adminSelectByExpertId(expertId);
        map.put("data", JsonUtil.obj2Json(expertReviewList));

        return "expertInfo/adminEdit/expertReview/index";
    }


    @RequiresPermissions("expertInfo:adminEdit:edit")
    @RequestMapping("editDlg")
    public String editDlg(String id, String expertId, Map map) throws Exception {
        if (StringUtils.isNotEmpty(id)) {
            ExpertReview expertReview;

            expertReview = expertReviewServices.selectByPrimaryKey(id);

            if (expertReview == null) {
                map.put(StaticConstant.ERROR_MESSAGE, StaticConstant.NOT_FOUND_DATA);
                return StaticConstant.ERROR_DLG;
            }
            map.put(StaticConstant.EDIT_DATA, expertReview);
            map.put(StaticConstant.EDIT_URL, "expertInfo/adminEdit/expertReview/update");
            map.put(StaticConstant.EDIT_OPERATE_TYPE, StaticConstant.EDIT_UPDATE);
        } else {
            ExpertReview expertReview = new ExpertReview();
            expertReview.setExpertId(expertId);
            map.put(StaticConstant.EDIT_DATA, expertReview);
            map.put(StaticConstant.EDIT_URL, "expertInfo/adminEdit/expertReview/insert");
            map.put(StaticConstant.EDIT_OPERATE_TYPE, StaticConstant.EDIT_INSERT);
        }
        return "expertInfo/adminEdit/expertReview/editDlg";
    }

    @RequiresPermissions("expertInfo:adminEdit:edit")
    @ResponseBody
    @RequestMapping("insert")
    public CommonResult insert(ExpertReview expertReview) throws Exception {
        CommonResult commonResult = CommonResult.newCommonResult();
        ComplexResult result = FluentValidator.checkAll().failOver()
//                .on(expertReview.getCompany(), new NotBlankFluentValidator("评审单位"))
                .on(expertReview.getEndTime(), new EndTimeFluentValidator(expertReview.getStartTime(), "开始时间", "结束时间", false))


                .doValidate()
                .result(ResultCollectors.toComplex());
        if (!result.isSuccess()) {
            commonResult.addMessage(result);
            return commonResult.setError();
        }


        expertReviewServices.adminInsertSelective(expertReview);

        return commonResult.setSuccess();
    }


    @RequiresPermissions("expertInfo:adminEdit:edit")
    @ResponseBody
    @RequestMapping("update")
    public CommonResult update(ExpertReview expertReview) throws Exception {
        CommonResult commonResult = CommonResult.newCommonResult();
        ComplexResult result = FluentValidator.checkAll().failOver()
//                .on(expertReview.getCompany(), new NotBlankFluentValidator("评审单位"))
                .on(expertReview.getEndTime(), new EndTimeFluentValidator(expertReview.getStartTime(), "开始时间", "结束时间", false))

                .doValidate()
                .result(ResultCollectors.toComplex());
        if (!result.isSuccess()) {
            commonResult.addMessage(result);
            return commonResult.setError();
        }


        expertReviewServices.adminUpdateByPrimaryKeySelective(expertReview);

        return commonResult.setSuccess();
    }


    @RequiresPermissions("expertInfo:adminEdit:edit")
    @ResponseBody
    @RequestMapping("delete")
    public CommonResult delete(@RequestParam("id") String id) throws Exception {
        CommonResult commonResult = CommonResult.newCommonResult();
        if (StringUtils.isBlank(id)) {
            commonResult.addMessage(StaticConstant.DATA_IS_BLANK);
            return commonResult.setError();
        }

        expertReviewServices.adminDeleteByPrimaryKey(id);

        return commonResult.setSuccess();
    }


}
